package com.zzyl.service.impl.handler;

import com.zzyl.entity.Resource;
import com.zzyl.vo.TreeItemVo;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 过滤二级目录的链
 */
public class SecondResourceFilterHandler extends ResourceFilterHandler {

    private ResourceFilterHandler nextHandler;

    public void setNextHandler(ResourceFilterHandler nextHandler) {
        this.nextHandler = nextHandler;
    }


    /**
     * @param allList          所有的菜单数据
     * @param root             根节点
     * @param parentResourceNo
     */
    @Override
    public void filter(List<Resource> allList, TreeItemVo root, String parentResourceNo) {
        root.getChildren().stream().map(r -> {
            //3.1 遍历一级目录
           /* List<Resource> secondList = allList.stream().filter(item -> {
                //遍历所有的菜单数据
                if (item.getParentResourceNo().equals(r.getId())) {
                    return true;
                } else {
                    return false;
                }
            }).collect(Collectors.toList());*/
            List<Resource> secondList = listResource(allList, r.getId());

            /*List<TreeItemVo> secondTreeItemVo = secondList.stream().map(i -> {
                TreeItemVo v = new TreeItemVo();
                v.setId(i.getResourceNo());
                v.setLabel(i.getResourceName());
                return v;
            }).collect(Collectors.toList());*/
            List<TreeItemVo> secondTreeItemVo =  parseItemVo(secondList);

            r.setChildren(secondTreeItemVo);
            return r;
        }).collect(Collectors.toList());

        if(null != nextHandler){
            nextHandler.filter(allList, root, parentResourceNo);
        }
    }
}
